using System;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
using System.Collections.Generic;
using SaturnExplorer.Virtualization;

namespace SaturnExplorer.Data
{
    public class MediaElementItemsProvider : IItemsProvider<MediaElementModel>
    {
        public int FetchCount()
        {
            using (LinqModelsDataContext dc = new LinqModelsDataContext())
            {
                int count =  dc.Mes.Count();
                return count;
            }
        }

        public IList<MediaElementModel> FetchRange(int startIndex, int count)
        {
            using (LinqModelsDataContext dc = new LinqModelsDataContext())
            {
                var elements = dc.Mes
                    .Skip(startIndex)
                    .Take(count)
                    .Select(x => GetMediaElement(dc, x))
                    .ToList();

                return elements;
            }
        }

        private MediaElementModel GetMediaElement(LinqModelsDataContext dc, Me me)
        {
            var result = new MediaElementModel(me.Alias);

            //if (me.idMe.ToString() == "051df495-8961-439f-a362-01aa0376f5b3")
            //{
            //    var fileBody = (from f in dc.MePreviewFiles
            //                    where f.idMe == me.idMe
            //                    select f).FirstOrDefault();

            //    if (fileBody != null)
            //    {
            //        using (LinqFilesDataContext fdc = new LinqFilesDataContext())
            //        {
            //            var body = (from b in fdc.MePreviewFileBodies
            //                        where b.idMePreviewFileBody == fileBody.idMePreviewFileBody
            //                        select b.body).FirstOrDefault();

            //            if (body != null)
            //            {
            //                //result.PreviewBody = body.ToArray();
            //            }
            //        }
            //    }
            //}

            return result;
        }

    }
}